import { Component, Host } from '@angular/core';
import { AppComponent } from '../../../app.component';
import { AuthenticationService } from '../login.service';
import { UtilsService } from '../../../shared/services/utils.service';

/**
 * Password reset component
 *
 * Author: Sorin Spiru, Andrei Istrate
 * Date: 13.01.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'forgotPass.component.html',
    styleUrls: ['../login.component.scss'],
    providers: [UtilsService]
})
export class ForgotPassComponent {
    public email: string = '';
    public message: string = '';
    public isError: boolean = true;
    public confirmed: boolean = false;
    public sent: boolean = false;

    constructor(
        private authenticationService: AuthenticationService,
        @Host() private rootComponent: AppComponent) { }

    ngAfterViewInit() {
        this.rootComponent.finishLoading();
    }

    resetPass() {
        this.sent = true;
        //validate email
        if (UtilsService.IsValidEmail(this.email)) {
            this.isError = false;
            this.authenticationService.resetPassword(this.email).subscribe(_ => {
                //password sent
                this.confirmed = true;
            });
        }
        else {
            this.isError = true;
            this.sent = false;
            this.message = 'Please enter a valid email address.';
        }
    }
}